package com.edu.neusoft.phone_sale.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.neusoft.phone_sale.common.ResponseResult;
import com.edu.neusoft.phone_sale.dto.ProductsDTO;
import com.edu.neusoft.phone_sale.entity.Products;
import com.edu.neusoft.phone_sale.mapper.ProductsMapper;
import com.edu.neusoft.phone_sale.service.ProductsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 产品表，存储产品的详细信息(Products)表服务实现类
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {

    private static final Logger logger = LoggerFactory.getLogger(ProductsServiceImpl.class);

    @Autowired
    private ProductsMapper productsMapper;

    /**
     * 分页查询商品信息
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return 包含商品DTO列表的响应结果
     */
    public ResponseResult<List<ProductsDTO>> findByPage(int pageNum, int pageSize) {
        try {
            List<ProductsDTO> productsList = productsMapper.selectByPage(pageNum, pageSize);
            // 处理productsList，如转换格式或添加额外逻辑
            return ResponseResult.okResult(productsList);
        } catch (Exception e) {
            // 记录异常信息
            logger.error("Error occurred while fetching products by page: {}", e.getMessage(), e);
            // 返回错误信息
            return ResponseResult.errorResult(500, "查询失败：" + e.getMessage());
        }
    }
}